package com.lagou.article.service.impl;

import com.lagou.article.pojo.Article;
import com.lagou.article.repository.ArticleRepository;
import com.lagou.article.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * service 实现类
 *
 * @author zyt
 * @date 2020/6/21 19:00
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public Page<Article> findAllByPage(int pageNum, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.DESC, "created");

        // 从0开始
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return articleRepository.findAll(pageable);
    }

    @Override
    public List<Article> findAll() {
        return articleRepository.findAll();
    }

}
